package com.meowcook.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;


@Getter
@Setter
public class AppResult<T> {
    // 状态码
    @JsonInclude(JsonInclude.Include.ALWAYS) //在任何情况下都参与序列化
    private long code;

    //状态描述
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;

    //结果数据
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    //构造方法
    public AppResult() {}

    public AppResult(long code, String message) {
        this.code = code;
        this.message = message;
        this.data = null;
    }

    public AppResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    /**
     * 成功，不返回数据
     * @return AppResult
     * @param <T>
     */
    public static <T> AppResult<T> success() {
        return new AppResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    /**
     * 成功,返回数据
     * @param data 数据
     * @return AppResult
     * @param <T>
     */
    public static <T> AppResult<T> success(T data) {
        return new AppResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功,不返回数据，自定义消息
     * @param message 自定义消息
     * @return AppResult
     * @param <T>
     */
    public static <T> AppResult<T> success(String message) {
        return new AppResult<T>(ResultCode.SUCCESS.getCode(), message);
    }

    /**
     * 成功,返回数据，自定义消息
     * @param message 自定义消息
     * @param data 数据
     * @return AppResult
     * @param <T>
     */
    public static <T> AppResult<T> success(String message, T data) {
        return new AppResult<T>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败
     * @return AppResult
     * @param <T>
     */
    public static <T> AppResult<T> fail() {
        return new AppResult<T>(ResultCode.FAIL.getCode(), ResultCode.FAIL.getMessage());
    }

    /**
     * 失败，自定义消息
     * @param message 自定义消息
     * @return AppResult
     * @param <T>
     */
    public static <T> AppResult<T> fail(String message) {
        return new AppResult<T>(ResultCode.FAIL.getCode(), message);
    }

    /**
     * 失败，返回错误码
     * @param resultCode 错误码
     * @return AppResult
     * @param <T>
     */
    public static <T> AppResult<T> fail(ResultCode resultCode) {
        return new AppResult<T>(resultCode.getCode(), resultCode.getMessage());
    }
}
